<?php

/**
 * Created by PhpStorm.
 * User: My pc
 * Date: 2017/2/2/002
 * Time: 13:45
 */
// 广告实体类
class AdverModel extends Model {
    private $id;
    private $type;
    private $link;
    private $title;
    private $state;
    private $thumbnail;
    private $info;
    private $limit;
    
    // 拦截器(__set)
    public function __set($_key, $_value) {
        $this->$_key = Tool::mysqlString($_value);
    }

    // 拦截器(__get)
    public function __get($_key) {
        return $this->$_key;
    }

    //查找单一广告
    public function getOneAdver() {
        $_sql = "SELECT 
                                id,
                                title,
                                info,
                                link
                    FROM 
                                cms_adver
                WHERE 
                                id='$this->id' 
                    LIMIT 
                                1";
        return parent::one($_sql);
    }

    //确定广告
    public function setStateOK() {
        $_sql = "UPDATE 
                                cms_adver 
                    SET 
                                state=1 
                WHERE 
                                id='$this->id' 
                    LIMIT 
                                1";
        return parent::aud($_sql);
    }

    //取消广告
    public function setStateCancel() {
        $_sql = "UPDATE 
                                cms_adver 
                    SET 
                                state=0 
                WHERE 
                                id='$this->id' 
                    LIMIT 
                                1";
        return parent::aud($_sql);
    }


    //获取广告总记录
    public function getAdverTotal() {
        $_sql = "SELECT 
                            COUNT(*) 
                FROM 
                            cms_adver";
        return parent::total($_sql);
    }

    //查询所有广告
    public function getAllAdver() {
        $_sql = "SELECT 
                                    id,
                                    title,
                                    link,
                                    type,
                                    state 
                    FROM 
                                    cms_adver
                ORDER BY
                                    date DESC
                    $this->limit";
        return parent::all($_sql);
    }

    //新增
    public function addAdver() {
        $_sql = "INSERT INTO 
                        cms_adver (
                                            title,
                                            link,
                                            thumbnail,
                                            info,
                                            type,
                                            state,
                                            date
                                    ) 
                    VALUES (
                                            '$this->title',
                                            '$this->link',
                                            '$this->thumbnail',
                                            '$this->info',
                                            '$this->type',
                                            1,
                                            NOW()
                                    )";
        return parent::aud($_sql);
    }

    //删除
    public function deleteAdver() {
        $_sql ="DELETE FROM 
                                    cms_adver 
                        WHERE 
                                    id='$this->id' 
                        LIMIT 1";
        return parent::aud($_sql);
    }
}